package com.atguigu.aliyunvod;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import org.junit.Test;

import java.util.List;

public class VodSdkTest {
    public static DefaultAcsClient initVodClient(String accessKeyId,String accessKeySecret){
        String regionId = "cn-shanghai"; // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId,accessKeyId,accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    //获取播放地址函数
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws ClientException {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId("5967ad83f5604ec2a78c8f53302ea2af"); //视频ID
        request.setResultType("Multiple"); //如果视频id是加密视频的id，则需要设置此参数，但只能获取文件无法解密播放
        return client.getAcsResponse(request);
    }

    @Test
    public void testGetPlayInfo(){
        DefaultAcsClient client = initVodClient("LTAI5tMdNwtR2LgFvLdhdsgp","xanEbkEDXQ7O3dvoW12sOHBsLSyOBD");
        GetPlayInfoResponse response = new GetPlayInfoResponse();

        try {
            response = getPlayInfo(client);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.println("PlayInfo.PlayURL = "+playInfo.getPlayURL()+"\n");
            }
            //Base信息
            System.out.println("VideoBase.Title = "+response.getVideoBase().getTitle() +"\n");

        } catch (Exception e) {
            System.out.println("ErrorMessage = "+e.getLocalizedMessage());
        }
        System.out.println("RequestId = "+response.getRequestId()+"\n");
    }

    /*
    *   获取视频播放凭证：加密视频必须使用此方式播放
    * */
    public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client) throws ClientException {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId("5967ad83f5604ec2a78c8f53302ea2af");
        request.setAuthInfoTimeout(3000L);
        return client.getAcsResponse(request);
    }

    //以下为调用示例
    @Test
    public void testGetVideoPlayAuth(){
        DefaultAcsClient client = initVodClient("LTAI5tMdNwtR2LgFvLdhdsgp","xanEbkEDXQ7O3dvoW12sOHBsLSyOBD");
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            response = getVideoPlayAuth(client);
            //播放凭证
            //eyJTZWN1cml0eVRva2VuIjoiQ0FJU2h3TjFxNkZ0NUIyeWZTaklyNWVNZjg3SHZZdEMzSlBTWjA3V25GRmlPL2grMXBQdGl6ejJJSDVFZW5OcUF1d2F2Lzh5bEd0VDZQZ1psck1xR3NVWUdoYWFNSkVyc2N3R3JsbitKcExGc3QySjZyOEpqc1VoamJzWjBsbXBzdlhKYXNEVkVmbDJFNVhFTWlJUi8wMGU2TC8rY2lyWXBUWEhWYlNDbFo5Z2FQa09Rd0M4ZGtBb0xkeEtKd3hrMnQxNFVtWFdPYVNDUHdMU2htUEJMVXhtdldnR2wyUnp1NHV5M3ZPZDVoZlpwMXI4eE80YXhlTDBQb1AyVjgxbExacGxlc3FwM0k0U2M3YmFnaFpVNGdscjhxbHg3c3BCNVN5Vmt0eVdHVWhKL3phTElvaXQ3TnBqZmlCMGVvUUFQb3BGcC9YNmp2QWF3UExVbTliWXhncGhCOFIrWGo3RFpZYXV4N0d6ZW9XVE84MCthS3p3TmxuVXo5bUxMZU9WaVE0L1ptOEJQdzQ0RUxoSWFGMElVRUJ5RUd5RWNQSDZwd3FTUFZqK0U1TG9pdjltamNCSHFIeno1c2VQS2xTMVJMR1U3RDBWSUpkVWJUbHphVTVJaHphNkx2TlhLRkVjS0FnNVdlMlBNYXgzYlFGRHI1M3ZzVGJiWHpaYjBtcHR1UG56ZDEwMVJuWGh4eUdWR29BQlprQjMxZXVuWi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WVoZ3hRNzd3UjlWRmkiLCJQbGF5RG9tYWluIjoic2gwMzI1bHN4Lmd1bGlvbmxpbmUuc2hvcCIsIkFjY2Vzc0tleVNlY3JldCI6IjV5VDVERWkza21RSjRyaUcyMnZVUDZBbUMydlZIdXROcnNxZW9WRXhVWHprIiwiUmVnaW9uIjoiY24tc2hhbmdoYWkiLCJDdXN0b21lcklkIjoxOTI1NzI2NTIzNzA5NzI3fQ==
            System.out.println("PlayAuth = "+ response.getPlayAuth() + "\n");
            //VideoMeta信息
            System.out.println("VideoMeta.Title = "+ response.getVideoMeta().getTitle()+"\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.println("RequestId = "+response.getRequestId()+"\n");
    }
}
